package com.guoranhao.web.util;

import com.guoranhao.tool.utils.LogUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

/**
 * Description: 文件分隔符处理
 *
 * @Version 1.0  2018/4/19.by 王红伟(wanghw@cloud-young.com) 创建
 */
public class FilePathUtil {

    private static final Logger logger = LoggerFactory.getLogger(FilePathUtil.class);

    //文件分隔符
    public static final String FILE_SEPARATOR = System.getProperty("file.separator");

    public static String getRealFilePath(String filePath){
        if(OSUtil.isWindowsOS()){
            return filePath.replaceAll("/", FILE_SEPARATOR);
        }else{
            return filePath.replaceAll("\\\\", FILE_SEPARATOR);
        }
    }

    /**
     *  删除文件
     * @param dir 文件夹
     * @param fileName 文件名
     * @return
     */
    public static boolean deleteFile(String dir,String fileName){
        LogUtil.info(logger,"文件夹路径{0},文件名称{1}",dir,fileName);
        String realFilePath = getRealFilePath(dir);

        File file = new File(dir,fileName);
        if(file.exists()){
            file.delete();
            LogUtil.info(logger,"删除文件成功");
            return  true;
        }
        LogUtil.error(logger,"文件不存在,文件夹路径{0},文件名称{1}",dir,fileName);
       return  false;
    }


}
